charset="UTF-8"
Ext.onReady(function(){
	Ext.QuickTips.init();
     // example of custom renderer function
    function change(val){
        if(val > 0){
            return '<span style="color:green;">' + val + '</span>';
        }else if(val < 0){
            return '<span style="color:red;">' + val + '</span>';
        }
        return val;
    }

    // example of custom renderer function
    function pctChange(val){
        if(val > 0){
            return '<span style="color:green;">' + val + '%</span>';
        }else if(val < 0){
            return '<span style="color:red;">' + val + '%</span>';
        }
        return val;
    }
    

    //查询患者面板
    var queryPatientForm = new Ext.FormPanel({
        labelAlign: 'top',
        frame:true,
        title: '查询条件',
        bodyStyle:'padding:5px 5px 0',
        width:780,
        collapsible: true,//最小化
        items: [{
            layout:'column',
            items:[{
                columnWidth:.25,
                layout: 'form',
                items: [{
                    xtype:'textfield',
                    maxLength:20,
                    maxLengthText:'最大长度20',
                    fieldLabel: '姓名',
                    id:'queryname',
                    name: 'name',
                    anchor:'90%'
                }]
            },{
                columnWidth:.25,
                layout: 'form',
                items: [{
                   xtype:'combo',  
                fieldLabel:"性别",  
                store:new Ext.data.JsonStore({  
                    data:{data:[  
                                {id:"male",value:"男"},{id:"female",value:"女"}]},  
                    root:"data",  
                    fields:["id","value"]  
                }),  
                editable:false,
                emptyText:"请选择性别",
                displayField:"value",  
                hiddenName: "sex",
				valueField:"value",
                mode:"local",    
                triggerAction:"all",
                anchor:'90%'
                }]
            },{
            	columnWidth:.25,
                layout: 'form',
                items: [{
      			   xtype:'textfield',
                    maxLength:30,
                    maxLengthText:'最大长度30',
                    fieldLabel: '身份证号码',
                    id:'queryidentify',
                    name: 'identify',
                    anchor:'90%'
      			}]
            }]
        }],
        
		monitorValid:true,
		
        buttons: [{
            text: '查询',
            formBind: true,
            handler:function(){  
            	patientStore.baseParams = {
        			name:queryPatientForm.getForm().findField('name').getValue(),
                	sex:queryPatientForm.getForm().findField('sex').getValue(),
                	identify:queryPatientForm.getForm().findField('identify').getValue()
        		},
                patientStore.reload({params:{start:0,limit:15}});
                }
        },{
            text: '重置',
            handler:function(){
					queryPatientForm.form.reset();
					patientGrid.addBtn.setDisabled(true);
					patientStore.removeAll();
                }
        }]
    });
    

    
	//添加患者面板
    var addPatientForm = new Ext.FormPanel({
        labelAlign: 'top',
        frame:true,
        title: '患者信息',
        bodyStyle:'padding:5px 5px 0',
        width: 600,
        items:[
        	{layout:'column',border:false,labelSeparator:':',
             defaults:{layout: 'form',border:false,columnWidth:.25},
              items:[
                    {items: [{
                    	xtype:'textfield',
                   		allowBlank:false,
                    	blankText:'不能为空',
                    	maxLength:30,
                    	maxLengthText:'最大长度30',
                    	fieldLabel: '身份证号码',
                    	id:'addidentify',
                    	name: 'identify',
                    	anchor:'90%'
                    	}]},
                    {items: [{
						xtype:'textfield',
                   		allowBlank:false,
                    	blankText:'不能为空',
                    	maxLength:20,
                    	maxLengthText:'最大长度20',
                    	fieldLabel: '姓名',
                    	id:'addname',
                    	name: 'name',
                    	anchor:'90%'
                    	}]},
                    {items: [{
							inputType: 'password',
                			xtype:'textfield',
                    		allowBlank:false,
                    		blankText:'不能为空',
                    		minLength:4,
                    		minLengthText:'最小长度4',
                    		maxLength:20,
                    		maxLengthText:'最大长度20',
                    		fieldLabel: '密码',
                    		id: 'addpassword',
                    		name: 'password',
                    		anchor:'90%'
                    }]},
                    {items: [{
							inputType: 'password',
                			xtype:'textfield',
                    		vtype:'password',
                    		initialPassField: 'addpassword',
                    		allowBlank:false,
                    		blankText:'不能为空',
                    		minLength:4,
                    		minLengthText:'最小长度4',
                    		maxLength:20,
                    		maxLengthText:'最大长度20',
                    		fieldLabel: '重复密码',
                    		id:'addconfirmpassword',
                    		name: 'confirmpassword',
                    		anchor:'90%'
                    }]},
                    {items: [{
							xtype:'combo',  
                			fieldLabel:"性别",  
                			store:new Ext.data.JsonStore({  
                    		data:{data:[  
                                {id:"male",value:"男"},{id:"female",value:"女"}]},  
                    		root:"data",  
                    		fields:["id","value"]  
                			}),  
                			editable:false,
                			allowBlank:false,
                    		blankText:'不能为空',
                			emptyText:"请选择性别",
                			displayField:"value",  
                			hiddenName: "sex",
							valueField:"value",
                			mode:"local",    
                			triggerAction:"all",
                			anchor:'90%'
                	}]},
                    {items: [{
							xtype:'datefield',
                    		fieldLabel: '出生日期',
                    		id:'addbirthday',
                    		name: 'birthday',
                    		allowBlank:false,
                    		blankText:'不能为空',
                    		emptyText:'请选择日期', 
                    		invalidText : "{0} 是无效的日期 - 必须符合格式： {1}", 
							format : "Y-m-d" ,
							maxValue:new Date(),  
          					maxText:'时间不能超过今天',  
                    		anchor:'90%'
                    		}]},
                    {items: [{
							xtype:'textfield',
                   			allowBlank:false,
                    		blankText:'不能为空',
                    		maxLength:50,
                    		maxLengthText:'最大长度50',
                    		fieldLabel: '出生地',
                    		id:'addbirthplace',
                    		name: 'birthplace',
                    		anchor:'90%'
                    }]},
                    {items: [{
							xtype:'textfield',
                    		allowBlank:false,
                    		blankText:'不能为空',
                    		maxLength:20,
                    		maxLengthText:'最大长度20',
                    		fieldLabel: '民族',
                    		id:'addMZ',
                    		name: 'MZ',
                    		anchor:'90%'
                    }]},
                    {items: [{
							xtype:'checkbox',
                    		fieldLabel: '婚姻状况',
                    		boxLabel:'已婚',
                    		id:'addmarry',
                    		name: 'marry',
                    		anchor:'90%'
                    }]}, 
                    {items: [{
                    		xtype:'textfield',
                    		maxLength:10,
                    		maxLengthText:'最大长度10',
                    		fieldLabel: '血型',
                    		id:'addbloodtype',
                    		name: 'bloodtype',
                    		anchor:'90%'
                     }]},
                     {items: [{
                    		xtype:'textfield',
                    		maxLength:100,
                    		maxLengthText:'最大长度100',
                    		fieldLabel: '过敏史',
                    		id:'addGMS',
                    		name: 'GMS',
                    		anchor:'90%'
                     }]},
                     {items: [{
                    		xtype:'textfield',
                    		maxLength:100,
                    		maxLengthText:'最大长度100',
                    		fieldLabel: '家族史',
                    		id:'addJZS',
                    		name: 'JZS',
                    		anchor:'90%'
                     }]},
                    {items: [{
							xtype:'checkbox',
                    		fieldLabel: '开通状况',
                    		boxLabel:'开通',
                    		id:'addvalid',
                    		name: 'valid',
                    		anchor:'90%'
            		}]}
               ]}
               ],
		monitorValid:true,
		
        buttons: [{
            text: '保存',
            formBind: true,
            handler:function(){                   
            	addPatientForm.getForm().submit({//客户端的数据提交给服务器   
                     url:'addPatient.action',      
                     method:'post',
                     waitTitle: '信息', 
                    waitMsg:"正在提交，请稍候。。。",  
                    success: function(form, action){   
                         Ext.MessageBox.alert('信息', '添加成功');
                         addPatientForm.form.reset();
                 		 addPatientWindow.hide();
                 		 patientStore.reload();
                      },
                     failure:function(form, action){  
                         Ext.MessageBox.alert('错误', '添加失败，请重试'); 
                         addPatientForm.form.reset();
                 		 addPatientWindow.hide();
                     }                                  
                 }); 
                 
                }
        },{
            text: '取消',
            handler:function(){
					addPatientForm.form.reset();
                    addPatientWindow.hide();
                }
        }]
    });
	//添加患者窗体
    var addPatientWindow = new Ext.Window({
	title : '添加患者',
	width : 615,
	height : 300,
	plain : true,
	closeAction : 'hide',// 关闭窗口
	maximizable : false,// 最大化控制 值为true时可以最大化窗体
	layout:'Form',
	items : [addPatientForm]
});
  //修改患者面板
    var updatePatientForm = new Ext.FormPanel({
        labelAlign: 'top',
        frame:true,
        title: '患者信息',
        bodyStyle:'padding:5px 5px 0',
        width: 600,
        items:[
        	{layout:'column',border:false,labelSeparator:':',
             defaults:{layout: 'form',border:false,columnWidth:.25},
              items:[
                    {items: [{
                    	xtype:'textfield',
                   		allowBlank:false,
                    	blankText:'不能为空',
                    	maxLength:30,
                    	maxLengthText:'最大长度30',
                    	fieldLabel: '身份证号码',
                    	id:'updateidentify',
                    	name: 'identify',
                    	anchor:'90%'
                    	}]},
                    {items: [{
						xtype:'textfield',
                   		allowBlank:false,
                    	blankText:'不能为空',
                    	maxLength:20,
                    	maxLengthText:'最大长度20',
                    	fieldLabel: '姓名',
                    	id:'updatename',
                    	name: 'name',
                    	anchor:'90%'
                    	}]},
                    {items: [{
							inputType: 'password',
                			xtype:'textfield',
                    		allowBlank:false,
                    		blankText:'不能为空',
                    		maxLength:20,
                    		maxLengthText:'最大长度20',
                    		fieldLabel: '密码',
                    		id: 'updatepassword',
                    		name: 'password',
                    		anchor:'90%'
                    }]},
                    {items: [{
							inputType: 'password',
                			xtype:'textfield',
                    		vtype:'password',
                    		initialPassField: 'updatepassword',
                    		allowBlank:false,
                    		blankText:'不能为空',
                    		maxLength:20,
                    		maxLengthText:'最大长度20',
                    		fieldLabel: '重复密码',
                    		id:'updateconfirmpassword',
                    		name: 'confirmpassword',
                    		anchor:'90%'
                    }]},
                    {items: [{
							xtype:'combo',  
                			fieldLabel:"性别",  
                			store:new Ext.data.JsonStore({  
                    		data:{data:[  
                                {id:"male",value:"男"},{id:"female",value:"女"}]},  
                    		root:"data",  
                    		fields:["id","value"]  
                			}),  
                			editable:false,
                			allowBlank:false,
                    		blankText:'不能为空',
                			emptyText:"请选择性别",
                			displayField:"value",  
                			hiddenName: "sex",
							valueField:"value",
                			mode:"local",    
                			triggerAction:"all",
                			anchor:'90%'
                	}]},
                    {items: [{
							xtype:'datefield',
                    		fieldLabel: '出生日期',
                    		id:'updatebirthday',
                    		name: 'birthday',
                    		allowBlank:false,
                    		blankText:'不能为空',
                    		emptyText:'请选择日期', 
                    		invalidText : "{0} 是无效的日期 - 必须符合格式： {1}", 
							format : "Y-m-d" ,
							maxValue:new Date(),  
          					maxText:'时间不能超过今天',  
                    		anchor:'90%'
                    		}]},
                    {items: [{
							xtype:'textfield',
                   			allowBlank:false,
                    		blankText:'不能为空',
                    		maxLength:50,
                    		maxLengthText:'最大长度50',
                    		fieldLabel: '出生地',
                    		id:'updatebirthplace',
                    		name: 'birthplace',
                    		anchor:'90%'
                    }]},
                    {items: [{
							xtype:'textfield',
                    		allowBlank:false,
                    		blankText:'不能为空',
                    		maxLength:20,
                    		maxLengthText:'最大长度20',
                    		fieldLabel: '民族',
                    		id:'updateMZ',
                    		name: 'MZ',
                    		anchor:'90%'
                    }]},
                    {items: [{
							xtype:'checkbox',
                    		fieldLabel: '婚姻状况',
                    		boxLabel:'已婚',
                    		id:'updatemarry',
                    		name: 'marry',
                    		anchor:'90%'
                    }]}, 
                    {items: [{
                    		xtype:'textfield',
                    		maxLength:10,
                    		maxLengthText:'最大长度10',
                    		fieldLabel: '血型',
                    		id:'updatebloodtype',
                    		name: 'bloodtype',
                    		anchor:'90%'
                     }]},
                     {items: [{
                    		xtype:'textfield',
                    		maxLength:100,
                    		maxLengthText:'最大长度100',
                    		fieldLabel: '过敏史',
                    		id:'updateGMS',
                    		name: 'GMS',
                    		anchor:'90%'
                     }]},
                     {items: [{
                    		xtype:'textfield',
                    		maxLength:100,
                    		maxLengthText:'最大长度100',
                    		fieldLabel: '家族史',
                    		id:'updateJZS',
                    		name: 'JZS',
                    		anchor:'90%'
                     }]},
                    {items: [{
							xtype:'checkbox',
                    		fieldLabel: '开通状况',
                    		boxLabel:'开通',
                    		id:'updatevalid',
                    		name: 'valid',
                    		anchor:'90%'
            		}]},
            		{items: [{
							 xtype:'textfield',
                    		hidden:true,
                    		id:'updateid',
                    		name: 'id',
                    		anchor:'90%'
            		}]}
               ]}
               ],
		monitorValid:true,
		
        buttons: [{
            text: '保存',
            formBind: true,
            handler:function(){                   
            	updatePatientForm.getForm().submit({//客户端的数据提交给服务器   
                     url:'updatePatient.action',      
                     method:'post',
                     waitTitle: '信息', 
                    waitMsg:"正在提交，请稍候。。。",  
                    success: function(form, action){   
                         Ext.MessageBox.alert('信息', '修改成功');
                         updatePatientForm.form.reset();
                 		 updatePatientWindow.hide();
                 		 patientStore.reload();
                      },
                     failure:function(form, action){  
                         Ext.MessageBox.alert('错误', '修改失败'); 
                         updatePatientForm.form.reset();
                 		 updatePatientWindow.hide();
                     }                                  
                 }); 
                 
                }
        },{
            text: '取消',
            handler:function(){
					updatePatientForm.form.reset();
                    updatePatientWindow.hide();
                }
        }]
    });
	//修改患者窗体
    var updatePatientWindow = new Ext.Window({
	title : '修改患者',
	width : 615,
	height : 300,
	plain : true,
	closeAction : 'hide',// 关闭窗口
	maximizable : false,// 最大化控制 值为true时可以最大化窗体
	layout:'Form',
	items : [updatePatientForm]
});
    var patientRecord = Ext.data.Record.create([
				{name:'id'},  
              {name:'identify'},
             {name:'name'},
              {name:'sex'},  
              {name:'birthday',type : 'date', dateFormat : 'Y-m-d' },  
              {name:'birthplace'},
              {name:'MZ'},
              {name:'marry'},  
              {name:'bloodtype'},  
              {name:'GMS'},  
              {name:'JZS'},  
              {name:'valid'}
		]);
    // create the data store
    var patientStore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({  
                	url:"queryPatientByPage.action",  
                    method:"post"  
                }),  
        reader : new Ext.data.JsonReader({
        	totalProperty : 'totalProperty',
        	root : 'patientVOList'
        	 }, 
        	 patientRecord
        	 )
    });

var marryRenderer = function(value) {
    if (value == 0) {
        return '未婚';
    } else if (value == 1) {
        return '已婚';
    }
};
var validRenderer = function(value) {
    if (value == 0) {
        return '未开通';
    } else if (value == 1) {
        return '已开通';
    }
};

	 var sm = new Ext.grid.CheckboxSelectionModel();  
	 
    var column = new Ext.grid.ColumnModel({
    		columns:[
    		new Ext.grid.RowNumberer(),
    		sm,
            {
            	header: "患者编号", 
            	width: 100, 
            	sortable: true, 
            	dataIndex: 'id'
            },
            {
            	header: "身份证号码",
            	sortable: true, 
            	width: 100, 
            	dataIndex: 'identify'
            },
            {
            	header: "姓名", 
            	sortable: true, 
            	width: 100, 
            	dataIndex: 'name'
            },
            {
            	header: "性别", 
            	width: 70, 
            	sortable: true, 
            	dataIndex: 'sex'
            },
            {
            	header: "出生日期", 
            	width: 120,
            	renderer: Ext.util.Format.dateRenderer('Y/m/d'), 
            	dataIndex: 'birthday'
            },
            {
            	header: "出生地", 
            	width: 100,
            	dataIndex: 'birthplace'
            },
            {
            	header: "民族", 
            	width: 120,
            	sortable: true,
            	dataIndex: 'MZ'
            },
            {
            	header: "婚姻状况", 
            	width: 120,
            	sortable: true,
            	renderer: marryRenderer, 
            	dataIndex: 'marry'
            },
            {
            	header: "血型", 
            	width: 100,
            	sortable: true,
            	dataIndex: 'bloodtype'
            },
            {
            	header: "过敏史", 
            	width: 200,
            	dataIndex: 'GMS'
            },
            {
            	header: "家族史", 
            	width: 200,
            	dataIndex: 'JZS'
            },
            {
            	header: "开通状态", 
            	width: 100,
            	renderer: validRenderer, 
            	dataIndex: 'valid'
            }
        ]});
        column.defaultSortable = true;//默认可排序  


        
    // create the Grid
    var patientGrid = new Ext.grid.GridPanel({
        id:'patient-grid',
        store: patientStore,
        cm: column,
        sm: sm,
        height:400,
        width:780,
        title:'患者信息列表',
		loadMask: {msg:'正在加载数据，请稍侯……'},
		collapsible: true,//最小化
        plugins: new Ext.ux.PanelResizer({
            minHeight: 100
        }),
        tbar: [{
        	ref: '../addBtn',
            iconCls: 'icon-add',
            text: '添加患者',
            handler: function(){
            	addPatientWindow.show();

            }
        },{
        	ref: '../updateBtn',
            iconCls: 'icon-update',
            text: '修改患者',
            disabled: true,
            handler: function(){
            	var s = patientGrid.getSelectionModel().getSelected();
            	updatePatientForm.getForm().findField('id').setValue(s.get('id'));
            	updatePatientForm.getForm().findField('identify').setValue(s.get('identify'));
            	updatePatientForm.getForm().findField('name').setValue(s.get('name'));
            	updatePatientForm.getForm().findField('sex').setValue(s.get('sex'));
            	updatePatientForm.getForm().findField('birthday').setValue(s.get('birthday'));
            	updatePatientForm.getForm().findField('birthplace').setValue(s.get('birthplace'));
            	updatePatientForm.getForm().findField('MZ').setValue(s.get('MZ'));
            	updatePatientForm.getForm().findField('marry').setValue(s.get('marry'));
            	updatePatientForm.getForm().findField('bloodtype').setValue(s.get('bloodtype'));
            	updatePatientForm.getForm().findField('GMS').setValue(s.get('GMS'));
            	updatePatientForm.getForm().findField('JZS').setValue(s.get('JZS'));
            	updatePatientForm.getForm().findField('valid').setValue(s.get('valid'));
            	
            	updatePatientWindow.show();
            }
        },{
        	ref: '../removeBtn',
            iconCls: 'icon-delete',
            text: '删除患者',
            disabled: true,
            handler: function(){
            	Ext.MessageBox.confirm("请确认","是否真的要删除指定的内容",function(button,text){
					if(button=="yes"){
						//执行删除操作
						 var s = patientGrid.getSelectionModel().getSelections();
                			for(var i = 0, r; r = s[i]; i++){
                    		Ext.Ajax.request({
     							method : 'POST',                             //发送方式.'POST'或'GET',一般是'POST'
     							url: 'deletePatient.action',                  //发送到页面
        						success:function(form,action){                   //发送成功的回调函数
       								Ext.Msg.alert('信息','删除成功');        //弹出对话框
       								patientStore.reload();                      //重新读入数据
      							},
     							failure:function(form,action){                          //发送失败的回调函数
       								Ext.Msg.alert("错误", "删除失败");
      							},
     							params:{id : r.get('id')} //发送参数
    						});

                		}
				
					}
				});

            }
        }],
        bbar: new Ext.PagingToolbar({
            pageSize: 15,
            store: patientStore,
            displayInfo: true,
			displayMsg:'显示第 {0} 条到 {1} 条记录，一共 {2} 条',  
            emptyMsg:'没有记录',
            plugins: new Ext.ux.ProgressBarPager()
        })
    });
    
    //选中以后激活删除按钮
    patientGrid.getSelectionModel().on('selectionchange', function(sm){
       
       	patientGrid.removeBtn.setDisabled(sm.getCount() < 1);
        patientGrid.updateBtn.setDisabled(sm.getCount() != 1);
    });
	

   patientGrid.render('patientGrid');
   queryPatientForm.render('queryPanel');
   
   
});